package mysql

import (
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	Name    string `gorm:"type:varchar(30);not null;comment:'药品名称'"`
	Types   string `gorm:"type:varchar(30);not null;comment:'药品规格'"`
	Froms   string `gorm:"type:varchar(30);not null;comment:'生产厂商'"`
	Comfort string `gorm:"type:varchar(30);not null;comment:'适应症'"`
	Num     int    `gorm:"type:int;not null;comment:'用法用量'"`
	Top     string `gorm:"type:varchar(50);not null;comment:'注意事项'"`
	UserId  int    `gorm:"type:int;not null;comment:'医生ID'"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func GoodsList(db *gorm.DB, id int64) []*doctorrpc.GoodsList {
	var list []*doctorrpc.GoodsList

	db.Debug().Model(Goods{}).
		Select("goods.name as name",
			"goods.types as types",
			"goods.froms as froms",
			"goods.comfort as comfort",
			"goods.num as num",
			"goods.top as top",
			"goods.user_id as user_id",
		).
		Where("goods.user_id = ?", id).
		Joins("left join users on goods.user_id = users.id").
		Find(&list)

	return list
}
